```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据库游标(Cursor)详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        h1, h2, h3, h4, h5, h6 {
            font-family: 'Noto Serif SC', serif;
            font-weight: 600;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #4361ee 0%, #3a0ca3 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
        }
        .sql-code {
            position: relative;
        }
        .sql-code::before {
            content: "SQL";
            position: absolute;
            top: 0;
            right: 0;
            background: #3a0ca3;
            color: white;
            padding: 2px 10px;
            font-size: 12px;
            border-bottom-left-radius: 4px;
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin-right: 0.1em;
            color: #3a0ca3;
            font-weight: 700;
        }
        .pros-cons-item {
            position: relative;
            padding-left: 2rem;
        }
        .pros-cons-item::before {
            position: absolute;
            left: 0;
            top: 0.3em;
            font-family: "Font Awesome 6 Free";
            font-weight: 900;
        }
        .pros-item::before {
            content: "\f058";
            color: #4ade80;
        }
        .cons-item::before {
            content: "\f057";
            color: #f87171;
        }
        .scroll-fade {
            opacity: 0;
            transform: translateY(20px);
            transition: opacity 0.6s ease, transform 0.6s ease;
        }
        .scroll-fade.active {
            opacity: 1;
            transform: translateY(0);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 md:py-32 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl text-center">
            <h1 class="text-4xl md:text-5xl font-bold mb-4">数据库游标(Cursor)详解</h1>
            <p class="text-xl md:text-2xl opacity-90 mb-8 max-w-3xl mx-auto">高效处理查询结果的数据库利器</p>
            <div class="inline-flex items-center justify-center space-x-4">
                <span class="bg-white bg-opacity-20 px-4 py-1 rounded-full text-sm flex items-center">
                    <i class="fas fa-database mr-2"></i> SQL 进阶
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-1 rounded-full text-sm flex items-center">
                    <i class="fas fa-code mr-2"></i> 开发技巧
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 md:px-0 py-12">
        <!-- Introduction -->
        <section class="mb-16 scroll-fade">
            <p class="text-lg text-gray-700 mb-6 drop-cap">数据库游标(Cursor)是一种用于逐行处理查询结果的数据库对象。它允许开发者在 SQL 查询中获取和操作结果集中的行。这在需要逐行处理数据或需要控制数据读取方式时尤其有用。</p>
            
            <div class="bg-blue-50 border-l-4 border-blue-500 p-4 my-6">
                <div class="flex">
                    <div class="flex-shrink-0 text-blue-500">
                        <i class="fas fa-info-circle text-xl"></i>
                    </div>
                    <div class="ml-3">
                        <p class="text-sm text-blue-700 font-medium">你知道吗？游标的概念类似于编程语言中的迭代器，允许你一次处理一个数据项，而不是整个集合。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Concept Section -->
        <section class="mb-16 scroll-fade">
            <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-blue-100 text-blue-600 rounded-full flex items-center justify-center mr-3">1</span>
                游标的基本概念
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-lg shadow-md p-6 card-hover">
                    <h3 class="text-lg font-semibold mb-3 text-blue-600 flex items-center">
                        <i class="fas fa-question-circle mr-2"></i> 定义
                    </h3>
                    <p class="text-gray-700">游标是一个数据库对象，它指向查询结果集的当前行，允许对结果集进行逐行处理和操作。</p>
                </div>
                
                <div class="bg-white rounded-lg shadow-md p-6 card-hover">
                    <h3 class="text-lg font-semibold mb-3 text-blue-600 flex items-center">
                        <i class="fas fa-tasks mr-2"></i> 作用
                    </h3>
                    <p class="text-gray-700">游标使得应用程序可以逐步检索和操作查询结果集中的数据，而不是一次性处理整个结果集。</p>
                </div>
            </div>
        </section>

        <!-- Workflow Section -->
        <section class="mb-16 scroll-fade">
            <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-blue-100 text-blue-600 rounded-full flex items-center justify-center mr-3">2</span>
                游标的工作流程
            </h2>
            
            <p class="text-gray-700 mb-6">游标的使用通常涉及以下几个步骤：</p>
            
            <div class="relative">
                <div class="absolute left-4 md:left-1/2 h-full w-0.5 bg-blue-200 -translate-x-1/2 hidden md:block"></div>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <!-- Step 1 -->
                    <div class="md:pr-12">
                        <div class="bg-white rounded-lg shadow-md p-6 card-hover relative">
                            <div class="absolute -left-4 md:-left-2 top-6 w-8 h-8 bg-blue-500 text-white rounded-full flex items-center justify-center font-bold">1</div>
                            <h3 class="text-lg font-semibold mb-3 text-blue-600">声明游标</h3>
                            <p class="text-gray-700 mb-4">定义游标并指定它所操作的查询语句。</p>
                            <div class="bg-gray-800 text-gray-100 p-4 rounded-md sql-code">
                                <pre class="overflow-x-auto"><code>DECLARE cursor_name CURSOR FOR SELECT_statement;</code></pre>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Step 2 -->
                    <div class="md:pl-12 md:mt-16">
                        <div class="bg-white rounded-lg shadow-md p-6 card-hover relative">
                            <div class="absolute -left-4 md:-left-2 top-6 w-8 h-8 bg-blue-500 text-white rounded-full flex items-center justify-center font-bold">2</div>
                            <h3 class="text-lg font-semibold mb-3 text-blue-600">打开游标</h3>
                            <p class="text-gray-700 mb-4">执行查询并将结果集与游标关联。</p>
                            <div class="bg-gray-800 text-gray-100 p-4 rounded-md sql-code">
                                <pre class="overflow-x-auto"><code>OPEN cursor_name;</code></pre>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Step 3 -->
                    <div class="md:pr-12 md:mt-16">
                        <div class="bg-white rounded-lg shadow-md p-6 card-hover relative">
                            <div class="absolute -left-4 md:-left-2 top-6 w-8 h-8 bg-blue-500 text-white rounded-full flex items-center justify-center font-bold">3</div>
                            <h3 class="text-lg font-semibold mb-3 text-blue-600">提取数据</h3>
                            <p class="text-gray-700 mb-4">从游标中逐行提取数据。通常使用 <code class="bg-gray-100 px-1 rounded">FETCH</code> 语句来获取当前游标指向的行的数据。</p>
                            <div class="bg-gray-800 text-gray-100 p-4 rounded-md sql-code">
                                <pre class="overflow-x-auto"><code>FETCH cursor_name INTO variable_list;</code></pre>
                            </div>
                        </div>
                    </div>
                    
                    <!-- Step 4 & 5 -->
                    <div class="md:pl-12 md:mt-16">
                        <div class="bg-white rounded-lg shadow-md p-6 card-hover relative">
                            <div class="absolute -left-4 md:-left-2 top-6 w-8 h-8 bg-blue-500 text-white rounded-full flex items-center justify-center font-bold">4</div>
                            <h3 class="text-lg font-semibold mb-3 text-blue-600">关闭游标</h3>
                            <p class="text-gray-700 mb-4">完成数据操作后，关闭游标，释放系统资源。</p>
                            <div class="bg-gray-800 text-gray-100 p-4 rounded-md sql-code mb-6">
                                <pre class="overflow-x-auto"><code>CLOSE cursor_name;</code></pre>
                            </div>
                            
                            <div class="absolute -left-4 md:-left-2 top-48 w-8 h-8 bg-blue-500 text-white rounded-full flex items-center justify-center font-bold">5</div>
                            <h3 class="text-lg font-semibold mb-3 text-blue-600">释放游标（可选）</h3>
                            <p class="text-gray-700 mb-4">在某些数据库系统中，还需要释放游标的资源。</p>
                            <div class="bg-gray-800 text-gray-100 p-4 rounded-md sql-code">
                                <pre class="overflow-x-auto"><code>DEALLOCATE cursor_name;</code></pre>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>

        <!-- Types Section -->
        <section class="mb-16 scroll-fade">
            <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-blue-100 text-blue-600 rounded-full flex items-center justify-center mr-3">3</span>
                游标的类型
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white rounded-lg shadow-md p-6 card-hover">
                    <h3 class="text-lg font-semibold mb-3 text-blue-600 flex items-center">
                        <i class="fas fa-eye-slash mr-2"></i> 隐式游标(Implicit Cursor)
                    </h3>
                    <p class="text-gray-700">通常由数据库系统自动管理，用于处理简单的 SQL 语句，如 <code class="bg-gray-100 px-1 rounded">SELECT</code>、<code class="bg-gray-100 px-1 rounded">INSERT</code>、<code class="bg-gray-100 px-1 rounded">UPDATE</code> 和 <code class="bg-gray-100 px-1 rounded">DELETE</code> 语句的结果。</p>
                </div>
                
                <div class="bg-white rounded-lg shadow-md p-6 card-hover">
                    <h3 class="text-lg font-semibold mb-3 text-blue-600 flex items-center">
                        <i class="fas fa-eye mr-2"></i> 显式游标(Explicit Cursor)
                    </h3>
                    <p class="text-gray-700">由开发者显式声明和管理，用于处理更复杂的查询或需要逐行操作的情况。显式游标提供更多的控制权和灵活性。</p>
                </div>
            </div>
        </section>

        <!-- Applications Section -->
        <section class="mb-16 scroll-fade">
            <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-blue-100 text-blue-600 rounded-full flex items-center justify-center mr-3">4</span>
                游标的应用场景
            </h2>
            
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-white rounded-lg shadow-md p-6 card-hover">
                    <div class="w-12 h-12 bg-blue-100 text-blue-600 rounded-full flex items-center justify-center mb-4">
                        <i class="fas fa-list-ol text-xl"></i>
                    </div>
                    <h3 class="text-lg font-semibold mb-2 text-gray-800">逐行处理</h3>
                    <p class="text-gray-700 text-sm">当需要逐行处理结果集中的数据时，例如计算或更新每一行的数据。</p>
                </div>
                
                <div class="bg-white rounded-lg shadow-md p-6 card-hover">
                    <div class="w-12 h-12 bg-blue-100 text-blue-600 rounded-full flex items-center justify-center mb-4">
                        <i class="fas fa-project-diagram text-xl"></i>
                    </div>
                    <h3 class="text-lg font-semibold mb-2 text-gray-800">复杂操作</h3>
                    <p class="text-gray-700 text-sm">在需要在查询结果上执行复杂的逻辑操作时，如执行多个 SQL 操作来处理每一行的数据。</p>
                </div>
                
                <div class="bg-white rounded-lg shadow-md p-6 card-hover">
                    <div class="w-12 h-12 bg-blue-100 text-blue-600 rounded-full flex items-center justify-center mb-4">
                        <i class="fas fa-sitemap text-xl"></i>
                    </div>
                    <h3 class="text-lg font-semibold mb-2 text-gray-800">业务逻辑</h3>
                    <p class="text-gray-700 text-sm">当业务逻辑要求逐步处理数据而不是一次性处理所有数据时，如在批处理操作中。</p>
                </div>
            </div>
        </section>

        <!-- Pros & Cons Section -->
        <section class="mb-16 scroll-fade">
            <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-blue-100 text-blue-600 rounded-full flex items-center justify-center mr-3">5</span>
                游标的优缺点
            </h2>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-green-50 rounded-lg border border-green-200 p-6">
                    <h3 class="text-xl font-semibold mb-4 text-green-700 flex items-center">
                        <i class="fas fa-thumbs-up mr-2"></i> 优点
                    </h3>
                    
                    <ul class="space-y-3">
                        <li class="pros-cons-item pros-item text-gray-700">
                            <strong>控制精度</strong>：提供了对结果集的精确控制，使得逐行处理数据成为可能。
                        </li>
                        <li class="pros-cons-item pros-item text-gray-700">
                            <strong>灵活性</strong>：允许开发者在遍历结果集时执行复杂的逻辑操作。
                        </li>
                    </ul>
                </div>
                
                <div class="bg-red-50 rounded-lg border border-red-200 p-6">
                    <h3 class="text-xl font-semibold mb-4 text-red-700 flex items-center">
                        <i class="fas fa-thumbs-down mr-2"></i> 缺点
                    </h3>
                    
                    <ul class="space-y-3">
                        <li class="pros-cons-item cons-item text-gray-700">
                            <strong>性能开销</strong>：游标操作可能会导致性能下降，尤其是对于大结果集，因为它们需要逐行读取数据。
                        </li>
                        <li class="pros-cons-item cons-item text-gray-700">
                            <strong>复杂性</strong>：使用游标需要更多的代码和管理，可能使应用程序变得复杂。
                        </li>
                    </ul>
                </div>
            </div>
        </section>

        <!-- Example Section -->
        <section class="mb-16 scroll-fade">
            <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <span class="w-8 h-8 bg-blue-100 text-blue-600 rounded-full flex items-center justify-center mr-3">6</span>
                示例代码
            </h2>
            
            <p class="text-gray-700 mb-6">以下是一个使用 MySQL 游标的示例：</p>
            
            <div class="bg-gray-800 text-gray-100 p-6 rounded-md sql-code">
                <pre class="overflow-x-auto"><code>-- 创建一个存储过程，演示如何使用游标
DELIMITER //

CREATE PROCEDURE process_data()
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE data_value VARCHAR(255);
    DECLARE cur CURSOR FOR SELECT data_column FROM my_table;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN cur;

    read_loop: LOOP
        FETCH cur INTO data_value;
        IF done THEN
            LEAVE read_loop;
        END IF;

        -- 在这里处理每一行的数据
        INSERT INTO processed_data (value) VALUES (data_value);
    END LOOP;

    CLOSE cur;
END //

DELIMITER ;</code></pre>
            </div>
            
            <div class="mt-6">
                <h3 class="text-lg font-semibold mb-3 text-gray-800">示例解析：</h3>
                <ul class="list-disc pl-5 space-y-2 text-gray-700">
                    <li>声明游标：<code class="bg-gray-100 px-1 rounded">DECLARE cur CURSOR FOR SELECT data_column FROM my_table;</code></li>
                    <li>打开游标：<code class="bg-gray-100 px-1 rounded">OPEN cur;</code></li>
                    <li>逐行提取数据：<code class="bg-gray-100 px-1 rounded">FETCH cur INTO data_value;</code></li>
                    <li>处理数据：在循环中对每一行数据执行插入操作</li>
                    <li>关闭游标：<code class="bg-gray-100 px-1 rounded">CLOSE cur;</code></li>
                </ul>
            </div>
        </section>

        <!-- Visualization Section -->
        <section class="mb-16 scroll-fade">
            <h2 class="text-2xl md:text-3xl font-bold mb-6 text-gray-800 flex items-center">
                <i class="fas fa-project-diagram mr-3 text-blue-500"></i>
                游标工作流程可视化
            </h2>
            
            <div class="bg-white rounded-lg shadow-md p-6">
                <div class="mermaid">
                    flowchart TD
                        A[声明游标\nDECLARE cursor] --> B[打开游标\nOPEN cursor]
                        B --> C[提取数据\nFETCH cursor]
                        C --> D{是否还有数据?}
                        D -->|是| E[处理当前行数据]
                        E --> C
                        D -->|否| F[关闭游标\nCLOSE cursor]
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-12">
        <div class="container mx-auto max-w-5xl px-4 md:px-0">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-6 md:mb-0">
                    <h3 class="text-xl font-bold text-white mb-2">技术小馆</h3>
                    <p class="text-gray-400">探索技术奥秘，分享开发经验</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition-colors flex items-center">
                        <i class="fas fa-external-link-alt mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        // Initialize Mermaid
        mermaid.initialize({
            startOnLoad: true,
            theme: 'dark',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });

        // Scroll animation
        document.addEventListener('DOMContentLoaded', function() {
            const scrollFadeElements = document.querySelectorAll('.scroll-fade');
            
            const observer = new IntersectionObserver((entries) => {
                entries.forEach(entry => {
                    if (entry.isIntersecting) {
                        entry.target.classList.add('active');
                    }
                });
            }, {
                threshold: 0.1
            });
            
            scrollFadeElements.forEach(element => {
                observer.observe(element);
            });
        });
    </script>
</body>
</html>
```